<template>
  <div>
    <div>
      <ViewPage :data="dataDict" :config="config" :model="model" />
    </div>
  </div>
</template>

<script>
import ViewPage from '@/components/ViewPage'
import objectMixin from './objectMixin'

// import api from '@/api'

export default {
  name: 'ObjectViewPage',
  components: { ViewPage },
  mixins: [objectMixin],

  data() {
    return {
      model: ''
    }
  },
  computed: {},

  async created() {
    this.init_header()
    this.init_config()

    this.init()
  },

  methods: {
    init_header() {
      const Model = this.Model
      const { id } = this.$route.query
      const rid = Number(id)

      // header title
      this.$store.dispatch(
        'header/setTitle',
        `${Model.metadata.description} 详情`
      )
      // header setRight

      // console.log('list, Model', Model)
      let header_right = {}
      if (Model.metadata.actions.write) {
        header_right = {
          path: {
            path: this.config.path.form,
            query: { model: this.model, id: rid }
          },

          icon: 'edit'
        }
      } else {
        header_right = undefined
      }

      this.$store.dispatch('header/setRight', header_right)
    },

    async init() {
      const model = this.model

      const { id } = this.$route.query
      const rid = Number(id)

      const fields = this.get_fieldsForBrowse()
      await this.$store.dispatch('object/browse_one', {
        _model: model,
        id: rid,
        fields
      })

      // const data = this.$store.state.object.dataDict
      console.log('View, data,', fields, this.dataDict)

      const res = await this.Model.call('preview_sale_order', [
        this.dataDict.id
      ])
      console.log('View, data,', res)
    },

    async onWizardCreate(method, payload) {
      //
      console.log('onWizardCreate', method, payload)

      const model = this.model

      const { id } = this.$route.query
      const rid = Number(id)

      const fields = this.get_fieldsForBrowse()

      const wizard_model = payload.wizard.model

      if (payload.wizard.submit) {
        const wizard_method = payload.wizard.method
        await this.$store.dispatch('object/wizard_create_submit_browse', {
          _model: model,
          id: rid,
          fields,
          wizard: { model: wizard_model, method: wizard_method }
        })
      } else {
        await this.$store.dispatch('object/wizard_create', {
          _model: model,
          id: rid,
          model: wizard_model
        })
      }
    },

    async onWriteLike(method) {
      //
      console.log('onWriteLike', method, id)

      const model = this.model

      const { id } = this.$route.query
      const rid = Number(id)

      const fields = this.get_fieldsForBrowse()
      await this.$store.dispatch('object/write_like', {
        _model: model,
        _method: method,
        id: rid,
        fields
      })
    }
  }
}
</script>

<style type="text/css"></style>
